#include "screen1.h"
#include <QDebug>

screen1::screen1(QWidget *parent, QUrl f) : Phonon::VideoWidget(0)
{
    this->parent = parent;
    fileLocation = f;

    mediaObject = new Phonon::MediaObject(this);
    mediaObject->setCurrentSource(Phonon::MediaSource(fileLocation));
    Phonon::createPath(mediaObject, this);
    connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(restartVideo()));

    audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);

    // path needed for audio manipulation
    path = Phonon::createPath(mediaObject, audioOutput);
    audioOutput->setVolume(0);

    // Flanger
    effectFlanger = new Phonon::Effect(Phonon::BackendCapabilities::availableAudioEffects()[8], this);
    effectFlanger->setParameterValue(effectFlanger->parameters()[6], 4);
    effectFlanger->setParameterValue(effectFlanger->parameters()[3], 10);

    // Echo
    effectEcho = new Phonon::Effect(Phonon::BackendCapabilities::availableAudioEffects()[6], this);
}

screen1::~screen1()
{
}

void screen1::restartVideo(){
    mediaObject->seek(0);
}

void screen1::playMedia(){
    mediaObject->play();
}

void screen1::pauseMedia(){
    mediaObject->pause();
}

void screen1::stopMedia(){
    mediaObject->stop();
}

void screen1::changeHue(qreal a){
    this->setHue(a);
}

void screen1::enableChorus(bool check){

}

void screen1::enableFlanger(bool check){
    if(check){
        path.insertEffect(effectFlanger);
    }
    else {
        path.removeEffect(effectFlanger);
    }
}

void screen1::enableEcho(bool check){
    if(check){
        path.insertEffect(effectEcho);
    }
    else {
        path.removeEffect(effectEcho);
    }
}

void screen1::enableGargle(bool check){

}

void screen1::keyPressEvent(QKeyEvent * event){
    parent->activateWindow();
}

void screen1::syncMedia(){
    mediaObject->seek(0);
}

void screen1::setMediaObject(QUrl f){
    mediaObject->setCurrentSource(Phonon::MediaSource(f));
    mediaObject->seek(0);
}
